package com.eflow.engine.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eflow.model.domain.EflowBytearray;
import com.eflow.engine.service.IEflowBytearrayService;
import com.eflow.model.expetion.FlowableException;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.DeploymentBuilder;
import org.flowable.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.eflow.engine.mapper.EflowModelMapper;
import com.eflow.model.domain.EflowModel;
import com.eflow.engine.service.IEflowModelService;

/**
 * 流程模型Service业务层处理
 *
 * @author eflow
 * @date 2021-02-09
 */
@Slf4j
public class EflowModelServiceImpl extends ServiceImpl<EflowModelMapper,EflowModel> implements IEflowModelService
{

    @Autowired
    private IEflowBytearrayService eflowBytearrayService;

    @Autowired
    private RepositoryService repositoryService;


    @Override
    public void saveModel(String id, String xml) {
        EflowModel eflowModel = getById(id);
        eflowModel.setVersion(eflowModel.getVersion()+1);
       Assert.notNull(eflowModel,"未找到模型数据");
       if(StrUtil.isBlank(eflowModel.getDeploymentId())){
           EflowBytearray eflowBytearray = new EflowBytearray();
           eflowBytearray.setName(eflowModel.getName());
           eflowBytearray.setBytes(xml);
           eflowBytearrayService.save(eflowBytearray);
           eflowModel.setDeploymentId(eflowBytearray.getId());
           updateById(eflowModel);
       }else {
           EflowBytearray bytearray = eflowBytearrayService.getById(eflowModel.getDeploymentId());
           Assert.notNull(bytearray,"未找到模型详细数据");
           bytearray.setBytes(xml);
           eflowBytearrayService.updateById(bytearray);
       }

    }

    @Override
    public Deployment deployModel(String id) {
        EflowModel eflowModel = getById(id);
        Assert.notNull(eflowModel,"未找到模型数据");
        EflowBytearray barry = eflowBytearrayService.getById(eflowModel.getDeploymentId());
        Assert.notNull(barry,"未找到模型详细数据");
        try {
            ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionKey(eflowModel.getKey()).latestVersion().singleResult();

            int version = 1;
            if(processDefinition!=null){
                version = processDefinition.getVersion()+1;
            }
            String name = eflowModel.getName() + DateUtil.format(new Date(),"yyyyMMdd") + "-" + version;
            DeploymentBuilder deploymentBuilder = repositoryService
                    .createDeployment()
                    .category(eflowModel.getCategory())
                    .key(eflowModel.getKey())
                    .name(name)
                    .addString( name+".bpmn20.xml", barry.getBytes());
            return deploymentBuilder.deploy();
        } catch (Exception e) {
            log.error("部署工作流失败" + e);
            throw new FlowableException("部署工作流失败");
        } // end catch
    }
}
